package com.linux.huhx.beanwrap;

import org.junit.Test;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.PropertyValue;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by huhx on 2017-05-17.
 */
public class BeanWrapTest {
    @Test
    public void beanWrap_1() {
        BeanWrapper wrapper = new BeanWrapperImpl(new Person());
        wrapper.setPropertyValue("username", "linux");
        PropertyValue passwordValue = new PropertyValue("password", "123456");
        wrapper.setPropertyValue(passwordValue);

        System.out.println(wrapper.getPropertyValue("username") + ", " + wrapper.getPropertyValue("password"));
    }
    // linux, 123456

    @Test
    public void expressionTest_1() {
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression("new String('hello world').toUpperCase()");
        String message = exp.getValue(String.class);
        System.out.println(message); // HELLO WORLD
        exp = parser.parseExpression("'Hello World'.bytes.length");
        int length = (Integer) exp.getValue();
        System.out.println(length); // 11
    }

    @Test
    public void resrouceTest_1() {
        Resource resource = new ClassPathResource("config/beanFactoryTest.xml");
        InputStream inputStream = null;
        try {
            inputStream = resource.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }
}
